package com.jiayou.im.controller

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.jiayou.im.entity.Operation
import com.jiayou.im.entity.Relation
import com.jiayou.im.service.impl.OperationServiceImpl
import com.jiayou.im.service.impl.RelationServiceImpl
import com.jiayou.im.service.impl.UserServiceImpl
import com.jiayou.im.vo.Resoult
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 *
 *
 * 前端控制器
 *
 *
 * @author lishuang
 * @since 2020-11-20
 */
@RestController
@RequestMapping("/im/operation")
open class OperationController {

    @Autowired
    private lateinit var operationServiceImpl: OperationServiceImpl

    @Autowired
    private lateinit var userServiceImpl: UserServiceImpl


    @Autowired
    private lateinit var relationServiceImpl: RelationServiceImpl

    //增加好友 || 删除好友
    @Transactional
    @PostMapping("getOperation")
    open fun operation(operation: Operation): Resoult<String> {
        if (userServiceImpl.getById(operation.id2) == null)
            return Resoult.fail("不存在该用户喔")

        if (operationServiceImpl.getOne(KtQueryWrapper<Operation>(operation)) != null) {
            return Resoult.fail("已经发送该请求了喔")
        }

        return if (operationServiceImpl.save(operation)) {
            relationServiceImpl.save(Relation(operation.id1, operation.id2))
            Resoult.success("执行操作完成")
        } else
            Resoult.fail("执行操作失败")
    }

}
